/**

 @Name：layuiAdmin（iframe版） 设置
 @Author：贤心
 @Site：http://www.layui.com/admin/
 @License: LPPL

 */
layui.config({
    base: '/layuiadmin/' //静态资源所在路径
}).extend({
    fileManager: 'lib/extend/fileManager' //主入口模块
}).define(['form','layer', 'fileManager','upload'], function(exports){
  var $ = layui.$
      ,fileManager=layui.fileManager
      ,upload=layui.upload
      ,layer=layui.layer;
    const fileChooser ={
        render:function(){
            $('[lgp-type="file_chooser"]').on('click',function(){
                let input_name = $(this).attr('lgp-name');
                let this_ =this;
                layer.open({
                    type: 1,
                    title: '文件选择',
                    area:['1000px','500px'],
                    content:'<button type="button" class="layui-hide" id="files"></button>\n' +
                        '<div class="layui-fluid">\n' +
                        '    <div id="fileManager" lay-filter="files"></div>\n' +
                        '</div>',
                    success:function(lay,index){
                        var upIns = upload.render({
                            elem: '#files' //绑定元素
                            , url: 'upload' //上传接口
                            , field: 'file[]'
                        });

                        fileManager.render({
                            elem: '#fileManager'
                            , method: 'post'
                            , id: 'fmfiles'
                            , btn_upload: true
                            , btn_create: true
                            , icon_url: '/upload/ico/'
                            , url: '/admin/file/get_file_data'
                            , thumb: {'nopic': '/logo.png', 'width': 100, 'height': 100}
                            , parseData: function (res) {
                                let _res = [];
                                _res.code = 0;
                                _res.data = res.images;
                                _res.count = res.count
                                return _res;
                            }
                            , done: function (res, curr, count) {
                                // console.log(res,curr,count)
                            }
                            , page: {limit: 100,layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']}
                            , where: {}
                        });


                        //监听图片选择事件
                        fileManager.on('pic(files)', function (obj) {
                            //obj.obj 当前对象
                            //obj.data 当前图片数据
                            let control = $(this_).find(`input[name=${input_name}]`);
                           /* let container = $(this_).find('.container');

                            if (container.length <= 0){
                                $(this_).append('<div class="container"></div>');
                            }*/
                            if (control.length <= 0){
                                $(this_).append(`<input type="hidden" name="${input_name}" >`);
                            }
                            control.val(obj.data.thumb);
                            let ext = {
                                'img':['jpg','png'],
                                 'video':['mp4']
                            };
                            if (ext['img'].includes(obj.data.type)){
                               /* $(this_).css('background-image',`url(${obj.data.thumb})`);
                                $(this_).css('background-repeat',`no-repeat`);
                                $(this_).css('background-size','100%  100%');*/
                               $(this_).find('.container').html(`<img src="${obj.data.thumb}">`);
                            }
                            if (ext['video'].includes(obj.data.type)){
                                $(this_).find('.container').html(`<video  style="width: 100%; height: 100%" controls>
                                            <source src="${obj.data.thumb}" type="video/${obj.data.type}">
                                        </video>`);
                            }
                            layer.close(index);

                        });
                        //监听图片上传事件
                        upIns.config.accept = 'file';
                        fileManager.on('uploadfile(files)', function (obj) {
                            //obj.obj 当前对象
                            //obj.path 路径
                            //更改上传组件参数
                            upIns.config.data = {'path': obj.path};
                            upIns.config.done = function (res) {
                            };
                            var e = document.createEvent("MouseEvents");
                            e.initEvent("click", true, true);
                            document.getElementById("files").dispatchEvent(e)
                        });
                        //监听新建文件夹事件
                        fileManager.on('new_dir(files)', function (obj) {
                            //obj.obj 当前对象
                            //obj.folder 文件夹名称
                            //obj.path 路径
                            $.post('folder', {'folder': obj.folder, 'path': obj.path}, function (e) {
                                let res = JSON.parse(e);
                                layer.msg(res.msg);
                                if (e.code == 0) {
                                    fileManager.reload('files');
                                }
                            })
                        });
                    }
                })
            });
        }
    };


  exports('fileChooser',fileChooser);
});
